package com.json.smtest.SMUtils.SM4_2;

import org.bouncycastle.util.encoders.Base64;

import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SM4Utils
{
    public String secretKey = "";
    public String iv = "";
    public boolean hexString = false;
    
    public SM4Utils()
    {
    }
    
    public String encryptData_ECB(String plainText)
    {
        try 
        {
            SM4_Context ctx = new SM4_Context();
            ctx.isPadding = true;
            ctx.mode = SM4.SM4_ENCRYPT;
            
            byte[] keyBytes;
            keyBytes = secretKey.getBytes();
            SM4 sm4 = new SM4();
            sm4.sm4_setkey_enc(ctx, keyBytes);
            byte[] encrypted = sm4.sm4_crypt_ecb(ctx, plainText.getBytes("UTF-8"));
//            String cipherText = new BASE64Encoder().encode(encrypted);
            byte[] cipherByte = Base64.encode(encrypted);
            String cipherText = new String(cipherByte);
            if (cipherText != null && cipherText.trim().length() > 0)
            {
                Pattern p = Pattern.compile("\\s*|\t|\r|\n");
                Matcher m = p.matcher(cipherText);
                cipherText = m.replaceAll("");
            }
            return cipherText;
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public String decryptData_ECB(String cipherText)
    {
        try 
        {
            SM4_Context ctx = new SM4_Context();
            ctx.isPadding = true;
            ctx.mode = SM4.SM4_DECRYPT;
            
            byte[] keyBytes;
            keyBytes = secretKey.getBytes();
            SM4 sm4 = new SM4();
            sm4.sm4_setkey_dec(ctx, keyBytes);
//            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, new BASE64Decoder().decodeBuffer(cipherText));
            byte[] decrypted = sm4.sm4_crypt_ecb(ctx, Base64.decode(cipherText));
            return new String(decrypted, "UTF-8");
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public String encryptData_CBC(String plainText){
         try{  
                SM4_Context ctx = new SM4_Context();  
                ctx.isPadding = true;  
                ctx.mode = SM4.SM4_ENCRYPT;  
                  
                byte[] keyBytes;  
                byte[] ivBytes;  
               
                keyBytes = secretKey.getBytes();  
                ivBytes = iv.getBytes();  
                  
                SM4 sm4 = new SM4();  
                sm4.sm4_setkey_enc(ctx, keyBytes);  
                byte[] encrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, plainText.getBytes("UTF-8"));  
//                String cipherText = new BASE64Encoder().encode(encrypted);
             byte[] cipherTByte = Base64.encode(encrypted);
             String cipherText = new String(cipherTByte);
             if (cipherText != null && cipherText.trim().length() > 0)
                {  
                    Pattern p = Pattern.compile("\\s*|\t|\r|\n");  
                    Matcher m = p.matcher(cipherText);  
                    cipherText = m.replaceAll("");  
                }  
                return cipherText;  
            }   
            catch (Exception e)   
            {  
                e.printStackTrace();  
                return null;  
            }  
        }  
          
        public String decryptData_CBC(String cipherText)
        {  
            try   
            {  
                SM4_Context ctx = new SM4_Context();  
                ctx.isPadding = true;  
                ctx.mode = SM4.SM4_DECRYPT;  
                  
                byte[] keyBytes;  
                byte[] ivBytes;  
                if (hexString)  
                {  
                    keyBytes = Util.hexStringToBytes(secretKey);  
                    ivBytes = Util.hexStringToBytes(iv);  
                }  
                else  
                {  
                    keyBytes = secretKey.getBytes();  
                    ivBytes = iv.getBytes();  
                }  
                  
                SM4 sm4 = new SM4();  
                sm4.sm4_setkey_dec(ctx, keyBytes);  
//                byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, new BASE64Decoder().decodeBuffer(cipherText));
                byte[] decrypted = sm4.sm4_crypt_cbc(ctx, ivBytes, Base64.decode(cipherText));
                return new String(decrypted, "UTF-8");
            }   
            catch (Exception e)  
            {  
                e.printStackTrace();
                return null;  
            }  
        }  
          
    
    public static void main(String[] args) throws IOException 
    {
        String plainText ="Keep自律给我自由!321";
        SM4Utils sm4 = new SM4Utils();
        sm4.secretKey = "d8f37edaf21f4373";
        plainText.getBytes("UTF-8");        
        System.out.println("ECB模式");
        String cipherText = sm4.encryptData_ECB(plainText);
        System.out.println("密文:" + cipherText);
        
        plainText = sm4.decryptData_ECB(cipherText);
        System.out.println("明文:" + plainText);
        
        System.out.println("CBC模式");  
        sm4.iv = "ed32fb5d34388842";
        cipherText = sm4.encryptData_CBC(plainText);  
        System.out.println("密文:" + cipherText);  
          
        plainText = sm4.decryptData_CBC(cipherText);  
        System.out.println("明文:" + plainText);
        
    }
}
